package com.qf.taobei.service.impl;

import com.qf.taobei.entity.User;
import com.qf.taobei.mapper.UserMapper;
import com.qf.taobei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        User us = userMapper.queryUserByTel(user.getTel());
        if (us == null){
            return  null;
        }
        if (!us.getPassword().equals(user.getPassword())){
            return null;
        }
        return us;
    }

    @Override
    public Integer register(User user) {
        Integer res = userMapper.addUser(user);
        return res;
    }

    @Override
    public Integer updateInfo(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Integer delUser(Integer id) {
        return userMapper.delUser(id);
    }

    @Override
    public List<User> searchUser(User user) {
        return userMapper.searchUser(user);
    }

    @Override
    public List<User> searchUserByuserInfo(String userInfo) {
        return userMapper.searchUserByuserInfo(userInfo);
    }

    @Override
    public User getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }

    @Override
    public Integer updateAccount(Integer id, Double money) {
        return userMapper.updateAccount(id,money);
    }

    @Override
    public boolean checkTel(String tel) {
        User user = new User();
        user.setTel(tel);
        List<User> users = userMapper.searchUser(user);
        //如果找不到，说明没有注册，或者是注册过但是已经被删除了,则可以注册
        if (users.size() == 0){
            return true;
        }
        return false;
    }

    @Override
    public Integer updatePwd(Integer id,String pwd) {
        return userMapper.updateUserPwd(id,pwd);
    }

    @Override
    public User queryUserByTel(String tel) {
        return userMapper.queryUserByTel(tel);
    }

    @Override
    public Integer updateTel(Integer id, String tel) {
        return userMapper.updateTel(id,tel);
    }

    @Override
    public Integer updateUserImg(Integer id, String url) {
        return userMapper.updateUserImg(id,url);
    }

    @Override
    public List<User> queryUserList() {
        return userMapper.queryUserList();
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }
}
